#coding:utf-8

from dagou.Agent import Agent
import urllib2

class TaobaoAgent(Agent):
    
    def __init__(self):
        Agent.__init__(self)
        self.agentId = 2
        self.baseUrl = "http://s.taobao.com/search?"
        self.code = 'gbk'
        self.site = '淘宝'
        self.ename = 'taobao'
#         self.ItemInfo = {'all' : "//div[@class='item-box']",
#                          'store' : (".//a[@trace='srpwwnick']", None),
#                          'name' : ("h3[1]/a[1]", 'title'),
#                          'price' : ("div[2]/div[1]", None),
#                          'link' : ("h3[1]/a[1]", 'href'),
#                          'carriage' : ("div[2]/div[2]", None),
#                          'volume' : ("div[3]/div[1]", None),
#                          'end': (('price', 'fun', "self.GetNumFromStr"), 
#                                  ('carriage', 'fun', "self.GetNumFromStr"),
#                                  ('volume', 'fun', "self.GetNumFromStr")),
#                          'fliter' : "price < 0.5 * oldprice",                         
#                          }

    def GetSearchList(self):
        while True:
            pri,form = self.que.get(1)
            if self.agentId not in form.agentIds:
#                 continue
                pass
            self.BeginProcForm(form)
            print "seraching ",form.name
            url = "q=%s&sort=price_asc" %form.name
            url = self.baseUrl + url
            req = urllib2.Request(url)
            result = self.opener.open(req).read()
#             open('tb.html','w').write(result)
            self.ParseResult(result, form, pri)
            self.EndProcForm(form)
            
